﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using PlanificadorDeEventos.Comun;
using System.Data.Entity;
using EventPlanner.Models;
using Microsoft.Practices.Unity;
using PlanificadorDeEventos.Interfaces;
using PlanificadorDeEventos.Repositorios;

namespace PlanificadorDeEventos
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            System.Data.Entity.Database.SetInitializer(new SampleData());
//          Database.SetInitializer<ModelContext>(new DropCreateDatabaseIfModelChanges<ModelContext>());

            IUnityContainer container = GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        }

        private IUnityContainer GetUnityContainer()
        {
            //Create UnityContainer          
            IUnityContainer container = new UnityContainer()
            .RegisterType<IControllerActivator, CustomControllerActivator>()
            .RegisterInstance<IRepositorioEventos>(new RepositorioEventos(ModelContext.Instance))
            .RegisterInstance<IRepositorioEventoUsuarioOrganizados>(new RepositorioEventoUsuarioOrganizados(ModelContext.Instance))
            .RegisterInstance<IRepositorioEventoUsuarioRegistrados>(new RepositorioEventoUsuarioRegistrados(ModelContext.Instance))
            .RegisterInstance<IRepositorioGuests>(new RepositorioGuests(ModelContext.Instance));
            return container;
        }

    }


    public class UnityDependencyResolver : IDependencyResolver
    {
        IUnityContainer container;
        public UnityDependencyResolver(IUnityContainer container)
        {
            this.container = container;
        }

        public object GetService(Type serviceType)
        {
            try
            {
                return container.Resolve(serviceType);
            }
            catch
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return container.ResolveAll(serviceType);
            }
            catch
            {
                return new List<object>();
            }
        }
    }

    public class CustomControllerActivator : IControllerActivator
    {
        IController IControllerActivator.Create(RequestContext requestContext, Type controllerType)
        {
            return DependencyResolver.Current.GetService(controllerType) as IController;
        }
    }
}